import type { ExtractProps } from '@/utils/type'
import styleVar from '@/styles/variable.module.scss'
import { toStrFirstCase } from '@/utils'
import type { PopupProps } from '@/uni_modules/lime-popup/components/l-popup/type'
import { Button } from '@/components'

namespace Popup {
  export const name = toStrFirstCase(`${styleVar.namespace}Popup`)
  export const props = {
    modelValue: Boolean,
    /**
     * 弹框标题
     */
    title: String,
    /**
     * 弹框底部按钮列表，handle 会传递 closePopup
     */
    btns: {
      type: Array<{
        text: string
        /** 如果不抛出异常会自动关闭弹框，如果抛出了异常，也可以使用 done 手动关闭 */
        handle?: (done: () => void) => void,
        props?: Button.Props,
        /** 此按钮开启 CatchError，默认 true */
        openLoading?: boolean
      }>,
      default: () => []
    },
    /**
     * 是否显示关闭按钮
     */
    showClose: {
      type: Boolean,
      default: true
    },
    /**
     * 是否允许在加载时关闭弹框，btns 里的按钮至少拥有一个 openLoading
     */
    loadingClose: Boolean
  }
  export const emits = {
    'update:modelValue': (_val: boolean) => true
  }
  export type Props = ExtractProps.Types<typeof props>
    & Partial<PopupProps> & PartialEmits<typeof emits> & PartialEmits<{
      clickOverlay: []
      clickClose: []
      open: []
      close: []
      opened: []
      closed: []
    }>
  export type Slots = {
    title: () => void,
    close: () => void,
    default: () => void,
    footer: () => void
  }
  export const Component = <{
    new(): {
      $props: Props
      $slots: Slots
    }
  }>{}
}
export default Popup